/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.ioHandling.fileReaders;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Properties;
import systole.ioHandling.logs.SystoleLogger;

/**
 * 
 * @author jmj
 */
public class FilePropertiesReader {

	/**
	 * Folder file settings
	 */
	public static final String PROPERTIES_FOLDER = "conf";
	/**
	 * File extension
	 */
	public static final String PROPERTIES_EXT = ".properties";

	/**
	 * @param propertieFileName
	 * @return properties loaded
	 */
	public Properties loadProperties(String propertieFileName) {
		FileInputStream in = null;
		String path = FilePropertiesReader.PROPERTIES_FOLDER
				+ System.getProperty("file.separator") + propertieFileName
				+ FilePropertiesReader.PROPERTIES_EXT;
		Properties properties = new Properties();
		File file = new File(path);
		try {
			if (!file.exists()) {
				file.createNewFile();
			}
			in = new FileInputStream(file);
			properties.load(in);
			in.close();
			return properties;
		} catch (FileNotFoundException ex) {
			SystoleLogger.getInstance()
					.logError("File not found, path " + path);
			return properties;
		} catch (IOException ex) {
			SystoleLogger.getInstance().logError("File can not be closed");
		}
		return properties;
	}

	/**
	 * @param properties
	 * @param propertieFileName
	 */
	public void saveProperties(Properties properties, String propertieFileName) {
		FileOutputStream out = null;
		String path = FilePropertiesReader.PROPERTIES_FOLDER
				+ System.getProperty("file.separator") + propertieFileName
				+ FilePropertiesReader.PROPERTIES_EXT;
		try {
			out = new FileOutputStream(path);
			properties.store(out, "Last change "
					+ Calendar.getInstance().getTime().toString());
			out.close();
		} catch (FileNotFoundException ex) {
			SystoleLogger.getInstance()
					.logError("File not found, path " + path);
		} catch (IOException ex) {
			SystoleLogger.getInstance().logError("Error on wirte file");
		}
	}
}